﻿package manager
{
    import com.adobe.utils.*;
    import com.sh.game.consts.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.loader.*;
    import flash.utils.*;

    public class XMLMcManager extends Object
    {
        private var _mcurls:Object;
        public var complatefuncs:Dictionary;
        private var _mcDic:Object;
        private var _dataDic:Object;
        private var _loadDic:Object;
        private var _ComplateFuncKeys:Array;
        private var _mainComplateFuncs:Object;
        public static var instance:XMLMcManager = new XMLMcManager;

        public function XMLMcManager()
        {
            this._mcurls = {goddess:["gold", "liquan", "qifu1", "qifu2"], qianghua:["newsHCsucces", "qh_jindutiao", "qh_quanquan", "zhuanyisuccess"], skill:["skillup"], wing:["guangyi"], qryl:["qryl_1", "qryl_3", "qryl_4", "qryl_5", "qryl_6", "qryl_7"], kaifujingji:["yifu1", "yifu2", "yifu3", "cibang", "zuoqi", "wuqi1", "wuqi2", "wuqi3"], weiduandenglu:["weiduan"], shouchong:["tulong", "fazhang", "yitian", "shouchong_cq"], xunbao:["xunbao"], newxunbao:["xunbao", "newxunbao1", "newxunbao2"], role:["baoshi", "ng_success1", "ng_success2", "ng_lizi", "flycg", "flyqq"], taskdialog:["taskstar1", "taskstar2", "taskanniu"], mainui:["qianghuasucces1", "qianghuaerror", "clearance"], leftkq:["leftzs"], guanwei:["chenggong"], achievement:["chenjiu"], shenlu:["guanwei"], lianzhiexp:["ding", "luzhi"], mrlc:["mrlc"], mobai:["mobaisuc", "mobaifail", "mobaieff"], union:["redpacket", "redpacketopen", "redpacketbtn"]};
            this.complatefuncs = new Dictionary();
            this._mcDic = new Object();
            this._dataDic = new Object();
            this._loadDic = new Object();
            this._ComplateFuncKeys = [];
            this._mainComplateFuncs = new Object();
            return;
        }// end function

        public function MainuiMc(param1:String, param2:Function) : void
        {
            var _loc_3:XMLMovieClip = null;
            if (this._dataDic[param1])
            {
                _loc_3 = new XMLMovieClip(this._dataDic[param1].data, null, null, true);
                var _loc_4:* = this._dataDic[param1];
                var _loc_5:* = this._dataDic[param1].count + 1;
                _loc_4.count = _loc_5;
                if (param2 != null)
                {
                    this.param2(_loc_3);
                }
            }
            else
            {
                if (this._mainComplateFuncs[param1] == null)
                {
                    this._mainComplateFuncs[param1] = [];
                }
                if (!ArrayUtil.arrayContainsValue(this._mainComplateFuncs[param1], param2))
                {
                    (this._mainComplateFuncs[param1] as Array).push(param2);
                }
                this.LoadDat("mainui", param1);
            }
            return;
        }// end function

        public function removeMainMc(param1:XMLMovieClip) : void
        {
            if (param1)
            {
                this.removemc(param1);
            }
            return;
        }// end function

        public function mcload(param1:String) : void
        {
            var _loc_3:String = null;
            var _loc_4:XMLMcLoader = null;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            if (!this._mcurls.hasOwnProperty(param1))
            {
                return;
            }
            if (this._ComplateFuncKeys.indexOf(param1) == -1)
            {
                this._ComplateFuncKeys.push(param1);
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._mcurls[param1].length)
            {
                
                _loc_3 = this._mcurls[param1][_loc_2];
                if (this._loadDic[_loc_3] == null)
                {
                    _loc_4 = new XMLMcLoader();
                    this._loadDic[_loc_3] = _loc_4;
                    _loc_4.load(Config.getUrl("assets/mcdat/" + _loc_3 + ".dat", ResourceType.NORMAL), 1, this.loaded, 0, [_loc_3]);
                }
                else if (this._dataDic[_loc_3])
                {
                    var _loc_8:* = this._dataDic[_loc_3];
                    var _loc_9:* = this._dataDic[_loc_3].count + 1;
                    _loc_8.count = _loc_9;
                    if (this.complatefuncs[param1])
                    {
                        _loc_5 = 0;
                        _loc_6 = this._mcurls[param1];
                        _loc_7 = 0;
                        while (_loc_7 < _loc_6.length)
                        {
                            
                            if (this._dataDic[_loc_6[_loc_7]] == null)
                            {
                                return;
                            }
                            _loc_7++;
                        }
                        (this.complatefuncs[param1] as Function).apply();
                    }
                }
                _loc_2++;
            }
            return;
        }// end function

        protected function LoadDat(param1:String, param2:String) : void
        {
            var _loc_4:XMLMcLoader = null;
            var _loc_3:* = this._loadDic[param2];
            if (_loc_3 == null)
            {
                _loc_4 = new XMLMcLoader();
                this._loadDic[param2] = _loc_4;
                _loc_4.load(Config.getUrl("assets/mcdat/" + param2 + ".dat", ResourceType.NORMAL), 1, this.loaded, 0, [param2]);
            }
            return;
        }// end function

        private function removemc(param1:XMLMovieClip) : void
        {
            if (param1)
            {
                if (param1.parent)
                {
                    param1.parent.removeChild(param1);
                }
                param1.dispose();
                param1 = null;
            }
            return;
        }// end function

        private function clearClsData(param1:String) : void
        {
            var _loc_2:int = 0;
            if (this._dataDic.hasOwnProperty(param1))
            {
                _loc_2 = this._dataDic[param1].count;
                _loc_2 = _loc_2 - 1;
                if (_loc_2 <= 0)
                {
                    (this._dataDic[param1].data as XMLClipData).dispose();
                    this._dataDic[param1] = null;
                    delete this._dataDic[param1];
                }
            }
            return;
        }// end function

        public function getPluginMc(param1:String, param2:String, param3:int = 0) : XMLMovieClip
        {
            var _loc_4:XMLMovieClip = null;
            if (this._dataDic[param2] == null)
            {
                return null;
            }
            if (this._mcDic[param1] == null)
            {
                this._mcDic[param1] = new Object();
            }
            if (this._mcDic[param1][param2] == null)
            {
                this._mcDic[param1][param2] = [];
            }
            if (this._mcDic[param1][param2][param3] == null)
            {
                var _loc_5:* = this._dataDic[param2];
                var _loc_6:* = this._dataDic[param2].count + 1;
                _loc_5.count = _loc_6;
                _loc_4 = new XMLMovieClip(this._dataDic[param2].data, null, null, true);
                this._mcDic[param1][param2][param3] = _loc_4;
            }
            return this._mcDic[param1][param2][param3];
        }// end function

        private function loaded(param1:String, param2:MovieAssets) : void
        {
            var _loc_3:Array = null;
            var _loc_4:XMLClipData = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Boolean = false;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:XMLMovieClip = null;
            if (this._dataDic[param1] == null && this._loadDic[param1])
            {
                _loc_4 = new XMLClipData(param2, param2.xml, true);
                param2.dispose();
                param2 = null;
                this._dataDic[param1] = {data:_loc_4, count:0};
                _loc_5 = [];
                if (this._ComplateFuncKeys.length > 0)
                {
                    _loc_6 = 0;
                    while (_loc_6 < this._ComplateFuncKeys.length)
                    {
                        
                        _loc_7 = 0;
                        _loc_3 = this._mcurls[this._ComplateFuncKeys[_loc_6]];
                        _loc_8 = true;
                        _loc_9 = 0;
                        while (_loc_9 < _loc_3.length)
                        {
                            
                            if (this._dataDic[_loc_3[_loc_9]] == null)
                            {
                                _loc_8 = false;
                                break;
                            }
                            _loc_9++;
                        }
                        if (_loc_8)
                        {
                            if (this.complatefuncs.hasOwnProperty(this._ComplateFuncKeys[_loc_6]))
                            {
                                (this.complatefuncs[this._ComplateFuncKeys[_loc_6]] as Function).apply();
                            }
                        }
                        else
                        {
                            _loc_5.push(this._ComplateFuncKeys[_loc_6]);
                        }
                        _loc_6++;
                    }
                    this._ComplateFuncKeys = _loc_5;
                }
                if (this._mainComplateFuncs[param1])
                {
                    _loc_3 = this._mainComplateFuncs[param1];
                    _loc_10 = 0;
                    while (_loc_10 < _loc_3.length)
                    {
                        
                        _loc_11 = new XMLMovieClip(this._dataDic[param1].data, null, null, true);
                        var _loc_12:* = this._dataDic[param1];
                        var _loc_13:* = this._dataDic[param1].count + 1;
                        _loc_12.count = _loc_13;
                        (_loc_3[_loc_10] as Function).apply(null, [_loc_11]);
                        _loc_10++;
                    }
                }
            }
            if (this._loadDic.hasOwnProperty(param1))
            {
                this._loadDic[param1] = null;
                delete this._loadDic[param1];
            }
            return;
        }// end function

        public function mcPluginDispose(param1:String) : void
        {
            var _loc_2:int = 0;
            var _loc_3:String = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            if (this._mcurls.hasOwnProperty(param1))
            {
                for each (_loc_3 in this._mcurls[param1])
                {
                    
                    if (this._mcDic[param1])
                    {
                        _loc_4 = this._mcDic[param1][_loc_3];
                        if (_loc_4)
                        {
                            _loc_5 = 0;
                            while (_loc_5 < _loc_4.length)
                            {
                                
                                this.removemc(_loc_4[_loc_5]);
                                this.clearClsData(_loc_3);
                                _loc_5++;
                            }
                        }
                        else if (this._dataDic[_loc_3])
                        {
                            _loc_2 = this._dataDic[_loc_3].count;
                            if (_loc_2 <= 0)
                            {
                                (this._dataDic[_loc_3].data as XMLClipData).dispose();
                                this._dataDic[_loc_3] = null;
                            }
                        }
                        continue;
                    }
                    if (this._dataDic[_loc_3])
                    {
                        _loc_2 = this._dataDic[_loc_3].count;
                        if (_loc_2 <= 0)
                        {
                            (this._dataDic[_loc_3].data as XMLClipData).dispose();
                            this._dataDic[_loc_3] = null;
                        }
                    }
                }
                delete this._mcDic[param1];
                ArrayUtil.removeValueFromArray(this._ComplateFuncKeys, param1);
            }
            return;
        }// end function

    }
}
